package pessoas.manipulaJar;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class LeitorJar {

    private File diretorio;
    private File[] arquivos;
    private Map<String, Map.Entry<String, URL>> classesPlugin;

    public LeitorJar(String folder) throws MalformedURLException, IOException, ClassNotFoundException {
        classesPlugin = new TreeMap<String, Map.Entry<String, URL>>();
        diretorio = new File(folder);
        FilenameFilter ff = new FilenameFilter() {
            @Override
            public boolean accept(File b, String name) {
                return name.endsWith(".jar");
            }
        };
        arquivos = diretorio.listFiles(ff);
        List<String> list = null;
        for (int i = 0; i < arquivos.length; ++i) {
            list = PackageUtils.getMainClass(arquivos[i]);
            Map.Entry<String, URL> infoClass = new AbstractMap.SimpleEntry<String, URL>(list.get(0), arquivos[i].toURI().toURL());
            classesPlugin.put(list.get(1), infoClass);
        }
    }

    public Map<String, Map.Entry<String, URL>> getClassesPlugin() {
        return classesPlugin;
    }
}
